package cn.itcast.web.servlet;

import javax.servlet.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

public class MyServlet implements Servlet {
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {

    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
//        System.out.println("你是谁");
        /*写,对象为服务器,写给服务器点东西
        PrintWriter writer = servletResponse.getWriter();
        writer.println("哈哈哈");*/
        /*给浏览器一个告知  告诉浏览器有一个 html类型的text文本文件,使用utf8解码   防止出现乱码*/
        servletResponse.setContentType("text/html;charset=utf-8");
        ArrayList<User> list = new ArrayList<>();
        list.add(new User(100, "张三", 20));
        list.add(new User(200, "李四", 22));
        list.add(new User(300, "王五", 18));
        list.add(new User(400, "赵六", 44));
        list.add(new User(500, "王八", 24));

        /*获取给服务器写的东西,并且起名out*/
        PrintWriter out = servletResponse.getWriter();
        /*println,写入html要写的东西*/
        out.println("<style>td{text-align: center;}</style>");
        out.println("<div style='width: 500px;height: 300px;margin: auto'>");
        out.println("<table style=\"width: 100%;height: 100%;\" border=\"1px\" cellpadding=\"0\" cellspacing=\"0\">");
        out.println("<tr><td>编号</td><td>姓名</td><td>年龄</td></tr>");

        /*增强for循环遍历 将数据添加进表格*/
        for (User user : list) {
            out.println("<tr>");
            out.println("<td>"+user.getId()+"</td>");
            out.println("<td>"+user.getName()+"</td>");
            out.println("<td>"+user.getAge()+"</td>");
            out.println("</tr>");
        }
        out.println("</table>");
        out.println("</div>");
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {

    }
}
